/*
 * @Author: wang cong wangc9832@163.com
 * @Date: 2023-07-11 17:18:35
 * @LastEditors: wang cong wangc9832@163.com
 * @LastEditTime: 2023-12-27 14:01:58
 * @FilePath: /ams_kt_front/src/utils/math.ts
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */
import { BigNumber } from 'bignumber.js';
//加
const add = (arg1: number, arg2: number) => {
  const p1 = new BigNumber(arg1);
  return p1.plus(arg2).toNumber();
};

//减
const subtract = (arg1: number, arg2: number) => {
  const p1 = new BigNumber(arg1);
  return p1.minus(arg2).toNumber();
};

//乘
const multiply = (arg1: number, arg2: number) => {
  const p1 = new BigNumber(arg1);
  return p1.multipliedBy(arg2).toNumber();
};

//除
const divide = (arg1: number, arg2: number) => {
  const p1 = new BigNumber(arg1);
  return p1.dividedBy(arg2).toNumber();
};

//数字四舍五入并保留两位小数，同时不足两位的部分用零进行补齐
function roundAndPadNumber(number, decimalPlaces) {
  const bigNumber = new BigNumber(number);
  const roundedNumber = bigNumber.toNumber();
  // 如果需要保留小数位数，可以使用 toFixed 方法
  if (!isNaN(decimalPlaces) && decimalPlaces >= 0) {
    return roundedNumber.toFixed(decimalPlaces);
  }
  return roundedNumber.toString();
}

export default {
  add,
  subtract,
  multiply,
  divide,
  roundAndPadNumber,
};
